package com.zhongfeng.api.modules.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongfeng.api.modules.business.mapper.BusinessMapper;
import com.zhongfeng.api.modules.business.service.BusinessService;
import com.zhongfeng.api.modules.business.vo.BusinessVo;
import com.zhongfeng.api.modules.business.vo.params.BusinessParams;
import com.zhongfeng.api.modules.end.mapper.EndMapper;
import com.zhongfeng.api.modules.end.service.EndService;
import com.zhongfeng.api.modules.end.vo.EndVo;
import com.zhongfeng.api.modules.end.vo.params.EndParams;
import com.zhongfeng.api.modules.utils.function.Transfer;
import com.zhongfeng.api.modules.utils.pojo.PageDataInfo;
import com.zhongfeng.api.modules.utils.pojo.Result;
import com.zhongfeng.api.modules.utils.service.HttpStatus;
import com.zhongfeng.api.modules.utils.service.ReturnCodes;
import com.zhongfeng.api.modules.utils.service.ReturnMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author blueCat
 * @date 2023/2/2 14:02
 * @motto life is a gift
 */
@Service
@Slf4j
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;

    /**
     * 查询全部营销网关
     *
     * @param params
     * @return
     */
    @Override
    public Result queryAllBusiness(BusinessParams params) {
        Page<BusinessVo> page = new Page<>(params.getPageNum(), params.getPageSize());
        List<BusinessVo> businessVos = businessMapper.queryAllBusiness(page, params);
        //回填信息
        for (BusinessVo businessVo : businessVos) {
            //回填对接信息
            businessVo.setPrice(Transfer.transferPrice(businessVo.getPriceDouble()));
            String joint = "对接IP和端口:" + businessVo.getIp() + ":2280\n"
                    +"被叫:"+businessVo.getAbbreviation()+"\n"
                    +"盲区:"+businessVo.getBlindArea()+"\n"
                    +"价格:"+businessVo.getPrice()+"\n"
                    +"频率:"+businessVo.getFrequency()+"\n"
                    +"时段:08:00---20:00\n"
                    +"查费链接:" + "http://" + businessVo.getIp() + ":28080" + "\n"
                    +"账号:" + businessVo.getCompany() +businessVo.getAbbreviation()+ "\n"+
                    "密码:888888";
            businessVo.setJoint(joint);
            //回填IP
            businessVo.setIp("http://" + businessVo.getIp() + ":28080");
            //回填查费信息
            String checkBill = "查费链接:" + businessVo.getIp() + "\n账号:" + businessVo.getCompany() +businessVo.getAbbreviation()+ "\n密码:888888";
            businessVo.setCheckBill(checkBill);
        }
        return Result.setSuccess(HttpStatus.SUCCESS, "", new PageDataInfo(page, businessVos));
    }

    /**
     * 新增营销网关
     *
     * @param params
     * @return
     */
    @Override
    public Result addBusiness(BusinessParams params) {
        int i = businessMapper.addBusiness(params);
        if (i == 1) {
            return Result.setSuccess(HttpStatus.SUCCESS, "添加营销网关成功！");
        } else {
            return Result.setFail(HttpStatus.FAIL, "添加营销网关失败！");
        }
    }

    /**
     * 删除营销网关
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteBusiness(Integer id) {
        int i = businessMapper.deleteBusiness(id);
        if (i == 1) {
            return Result.setSuccess(HttpStatus.SUCCESS, "删除营销网关成功！");
        } else {
            return Result.setFail(HttpStatus.FAIL, "删除营销网关失败！");
        }
    }

    /**
     * 修改营销网关
     *
     * @param params
     * @return
     */
    @Override
    public Result updateBusiness(BusinessParams params) {
        int i = businessMapper.updateBusiness(params);
        if (i == 1){
            return Result.setSuccess(HttpStatus.SUCCESS, "修改营销网关成功！");
        }else {
            return Result.setFail(HttpStatus.FAIL,"修改营销网关失败！");
        }
    }
}
